package great.project.jpa.signle.table;

import great.project.jpa.sign.table.SpringDataJpaSearchSingelTableDemo;
import great.project.jpa.sign.table.domain.entity.SysRole;
import great.project.jpa.sign.table.domain.respository.SysRoleRepository;
import great.project.jpa.sign.table.dto.RoleDto;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsNull;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 *  <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *  使用简单的 动态类型的查询
 *
 *  </div>
 * @author v_zhangshun01
 * @date 2021/8/20 11:14 上午
 */
@SpringBootTest(classes = SpringDataJpaSearchSingelTableDemo.class)
public class NameQuery {

    @Resource
    private SysRoleRepository sysRoleRepository;


    @Test
    void findFirstByIdOrderById() {
        final SysRole firstByIdOrderById = sysRoleRepository.findFirstByIdOrderById("1169504891467464705");
        MatcherAssert.assertThat(firstByIdOrderById.getId() , Is.is("1169504891467464705"));
    }

    @Test
    void countAll() {
        final Long aLong = sysRoleRepository.count();
        MatcherAssert.assertThat(aLong, Is.is(5L));
    }

    @Test
    void countByCreateBy() {
        final Long admin = sysRoleRepository.countByCreateBy("admin");
        MatcherAssert.assertThat(admin, Is.is(3L));

    }

    /**
     * 分页
     */
    @Test
    void findUpdateByPage() {
        final Page<SysRole> list = sysRoleRepository.findUpdateByPage("admin" , PageRequest.of(1,10));
        MatcherAssert.assertThat(list.getTotalElements(), Is.is(4L));
        list.stream().forEach(r->{
            MatcherAssert.assertThat(r.getUpdateBy(), Is.is("admin"));
        });
    }

 /**
  * 使用 实体类 命名查询
  */
    @Test
    void selectRoleNameForNamedQuery() {
        final List<String> list = sysRoleRepository.selectRoleNameForNamedQuery("admin");
        MatcherAssert.assertThat(list, IsNull.notNullValue());
        MatcherAssert.assertThat(list.get(0), Is.is("管理员"));
    }

    @Test
    void selectRoleFORNamedQuery() {
        final List<SysRole> list = sysRoleRepository.selectRoleForNamedQuery("admin");
        MatcherAssert.assertThat(list, IsNull.notNullValue());
        MatcherAssert.assertThat(list.get(0).getRoleName(), Is.is("管理员"));
    }

    @Test
    void selectRoleForNamedNativeQuery() {
        final List<RoleDto> list = sysRoleRepository.selectRoleForNamedNativeQuery("admin");
        MatcherAssert.assertThat(list, IsNull.notNullValue());
        System.out.println(list);
    }



}
